package org.fuys.etp.core.checker.impl;

import org.fuys.etp.core.checker.EtpUpdateChecker;
import org.fuys.etp.core.model.event.EtpAlertEvent;
import org.fuys.etp.core.model.res.TpConfigCheckResult;
import org.fuys.etp.core.model.res.TpStatusResult;
import org.fuys.etp.core.model.type.EtpTaskTrendTypeEnum;
import org.fuys.etp.core.model.vo.TpBaseConfigVO;
import org.fuys.etp.core.model.vo.TpRegulationStrategyVO;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @projectName: fuys-etp-final
 * @package: org.fuys.etp.core.checker.impl
 * @className: LevelAlertChecker
 * @author: WangJing
 * @description: 等级报警检查器
 * @date: 2025/2/18 22:22
 * @version: 1.0
 */
@Component
public class LevelAlertChecker implements EtpUpdateChecker {

    @Resource
    private ApplicationEventPublisher eventPublisher;

    @Override
    public void checkHandle(TpRegulationStrategyVO strategy, TpStatusResult result, TpConfigCheckResult check) {
        if(result.getTpStatusVO().getRejectCount() == null){
            return;
        }
        //首先判断更加大众的拒绝策略触发的报警
        final TpBaseConfigVO baseConfig = result.getConfigVO().getBaseConfig();
        final Integer rejectAlertThreshold = baseConfig.getRejectAlertThreshold();
        if(result.getTpStatusVO().getRejectCount() >= rejectAlertThreshold){
            EtpAlertEvent event = new EtpAlertEvent(this,result,strategy.getAlertType());
            eventPublisher.publishEvent(event);
            return;
        }
        //然后判断一下进行下一阶段的预警
        if(strategy.getAlertLevel()!=null &&
                EtpTaskTrendTypeEnum.INCREASE.getType().equals(result.getTpStatusVO().getTaskTrend())
                && strategy.getAlertLevel().equals(check.getSuggestConfig().getLoadLevel())){
            EtpAlertEvent event = new EtpAlertEvent(this,result,strategy.getAlertType());
            eventPublisher.publishEvent(event);
        }
    }
}
